﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.Core.DataAccess;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZJ_HZJEControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                BindData();
            }
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
            Button button1 = (Button)FindControl("button1");
            button1.Click+=new EventHandler(button1_Click);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Label labYe = (Label)FindControl("labYe");
            string strbid = base.Page.Request["ID"];
            string tt = _EluZSDataProvider.GetNowYE(strbid);
            labYe.Text = "当前余额为：" + string.Format("{0:N}", double.Parse(tt));
        }
        private void BindData()
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.RowDataBound+=new GridViewRowEventHandler(CustomGridView1_RowDataBound);
            string strPID = base.Page.Request["PID"];
            if (!string.IsNullOrEmpty(strPID))
            {
                DataTable dt = GetHZJE(strPID,GetStrWhere());
                if (dt.Rows.Count>0)
                {
                    CustomGridView1.DataSource = dt;
                    CustomGridView1.RecordCount = dt.Rows.Count;
                    CustomGridView1.HideColName = "id";
                    CustomGridView1.DataBind();
                }
             
            }
        }

        public DataTable GetHZJE(string pid, string strwhere)
        {
            string sql = @"select distinct t.征收批次号,t.PCID,t.PNID,
(select 立项号 from zs_project where id in 
(select pid from zs_prono where id=t.pnid) ) 立项号,
(select 项目名称 from zs_project where id in 
(select pid from zs_prono where id=t.pnid) ) 项目名称,
(select 征收许可证号 征收决定号 from zs_prono where id=t.pnid) 征收决定号,
(select id from zs_change_money where pcid=t.pcid ) cmid ,
(Select id From zs_bcbasicinfo Where Id = '" + base.Page.Request["ID"] + @"') id,
(Select id From zs_project Where Id = '" + pid + @"') pid
from zs_zspch t where t.提交状态=1 and t.pnid in (
select id from zs_prono where pid='" + pid + "') ";
            if (!string.IsNullOrEmpty(strwhere)) { sql += strwhere; }
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            return dt;
        }

        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }
        protected void CustomGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strPCID = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;
                string tt = _EluZSDataProvider.GetSomeStr("zs_zspch", "sum(to_number(nvl(划转金额,0)))", "pcid", strPCID);
                e.Row.Cells[6].Text = tt;
                string tt1 = _EluZSDataProvider.GetSomeStr("ZS_CHANGE_MONEY", "sum(to_number(nvl(划转金额,0)))", "pcid", strPCID);
                e.Row.Cells[7].Text = tt1;
                string tt2 = _EluZSDataProvider.GetSomeStr("ZS_CHANGE_MONEY", "to_char(划转时间,'yyyy-mm-dd')", "pcid", strPCID);
                e.Row.Cells[8].Text = tt2;
            }
        }
        protected void button1_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected string GetStrWhere() {
            TextBox 征收批次号 = (TextBox)FindControl("征收批次号");
            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(征收批次号.Text)) {
                strWhere += " and t.征收批次号 like '%" + 征收批次号.Text.Trim() + "%'";
            }
            return strWhere;
        }
    }
}


